package com.itmentu.listener.ack;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author：http://www.itmentu.com
 * @date： 2021/12/7 11:14
 * @Description：
 ***/
//@Component
public class ConfirmMessageListener {

    /**
     * 消息监听
     */
    //@RabbitListener(queues = "direct_publisher_queue")
    //public void listener(String message) {
    //    System.out.println("ConfirmMessageListener->监听到的消息是-->" + message);
    //    //制造异常
    //    int q=10/0;
    //}

    /**
     * 消息监听
     */
    @RabbitListener(queues = "direct_publisher_queue")
    public void listener(String message, Channel channel, Message msg) throws IOException {
        System.out.println("ConfirmMessageListener->手动确认->监听到的消息是-->" + message);
        //消息确认：false只确认当前consumer一个消息收到，true确认所有consumer获得的消息
        channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue topicQueue() {
        return QueueBuilder
                //指定队列名字
                .durable("direct_publisher_queue")
                //构建队列
                .build();
    }
}